/**
 * Created by ZFL on 2017/12/26.
 */
var _false2 = false, _false3 = false, _false4 = false, _false5 = false;
function appInit() {
    PageInit();
}
function PageInit() {
    if(com_obj.userId && com_obj.userId>0){
        $(".order-all a").click(function () {
            var _index=$(this).index()+1;
            $(this).addClass("textshow").siblings().removeClass("textshow");
            $(".tabs-" + _index + "").addClass("active").siblings().removeClass("active");
            switch (_index) {
                case 2:
                    if (!_false2) {
                        _false2 = true;
                        payGoods();
                    }
                    break;
                case 3:
                    if (!_false3) {
                        _false3 = true;
                        deliverGoods();
                    }
                    break;
                case 4:
                    if (!_false4) {
                        _false4 = true;
                        receiveGoods();
                    }
                    break;
                case 5:
                    if (!_false5) {
                        _false5 = true;
                        finishGoods();
                    }
                    break;
            }
        });
        infiniteSroll();
        orderAll();//全部订单
        if ($.getPara('pay') != '') {
            $(".order-all a").eq(1).click();
        } else if ($.getPara('deliver') != '') {
            $(".order-all a").eq(2).click();
        }
        else if ($.getPara('receive') != '') {
            $(".order-all a").eq(3).click();
        }
        else if ($.getPara('finish') != '') {
            $(".order-all a").eq(4).click();
        }
    }else {
        //var httpUrl = window.location.href;
        //window.location.href = '../user/user_login.html?href='+httpUrl+'';
    }

}
var app = new Vue({
    el:'#order-all',
    data:{
        allData:[],
        payData:[],
        sendData:[],
        receiveData:[],
        evaluateData:[]
    },
    methods:{

    }
});
//全部订单列表
var page1 = 1;
var pageSize1 = 15;
var loading1 = false;
function orderAll() {
    var _visible = 'show';
    if (loading1) {
        _visible = 'hide';
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageNum = page1;
            obj.pageSize = pageSize1;
            obj.status = 1;
            //obj.userId=2;
            if(com_obj.userId && com_obj.userId>0){
                obj.userId = com_obj.userId;
            }else{
                obj.userId = 0;
            }
            obj.storeId= $.getStorage('mallStoreId');
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        visible: '' + _visible + '',
        approve: true,
        api: "yg_baijiaplatform/order/selectOrderInfoByStatusAndUserId",
        success: function (result) {
            if (result.code == 10000) {
                var data = result.data;
                if (data.length > 0) {
                    $.each(data,function (index,item) {
                        var obj={};
                        obj.orderSn=item.orderSn;
                        obj.status=item.status;
                        obj.lian='./orderDetail.html?orderSn='+item.orderSn+'';
                        if(item.status==1){//待支付
                            obj.statusTex="待付款";
                            obj.lian='/b2c/pay/or_pay.html?orderSn='+item.orderSn+'';
                        }else if(item.status==2){//待发货
                            obj.statusTex="待发货";
                        }else if(item.status==3){//待收货
                            obj.statusTex="待收货";
                        }else if(item.status==4 ){
                            obj.statusTex="待评价";
                        }else if(item.status==6){
                            obj.statusTex="交易成功";
                        }else if(item.status==7){
                            obj.statusTex="交易关闭";
                        }
                        if(item.totalFee==0){
                            obj.totalFee = 0.01;
                        }else {
                            obj.totalFee=divideNumber(item.totalFee,100);//实付总计
                        }
                        obj.deliveryWay=item.deliveryWay;
                        var orderList=[];
                        if(item.listOrderDetail.length>0){
                            $.each(item.listOrderDetail,function (j,rows) {
                                var cobj={};
                                if(rows.navigateImg1){
                                    cobj.navigateImg1=rows.navigateImg1;
                                }
                                cobj.goodsName=rows.goodsName;
                                cobj.salePrice=divideNumber(rows.salePrice,100);
                                cobj.specName=rows.specName;
                                cobj.quantity=rows.quantity;
                                orderList.push(cobj);
                            });
                        }
                        obj.list = orderList;
                        app.allData.push(obj);
                    });
                    if(loading1){
                        loading1=false;
                    }
                } else {
                    loading1=true;
                    $(".tabs-1").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                        'style="display: inline-block;width: 113px;height: 100px;" ' +
                        'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
                    if(page1>1){
                        YDUI.dialog.toast('没有数据了','none',function(){});
                        YDUI.dialog.loading.close();
                    }
                }
            } else if (result.code == 20100) {
                YDUI.dialog.toast('用户认证失败，请重新登录','error',function(){});
            } else if (result.code == 20141) {
                $(".tabs-1").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                    'style="display: inline-block;width: 113px;height: 100px;" ' +
                    'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
            } else {
                YDUI.dialog.toast(result.message,'error',function(){});
            }
        }
    });
}

//待付款订单
var f_page=1;
var f_pageSize=15;
var loading2=false;
function payGoods(){
    var _visible = 'show';
    if (loading2) {
        _visible = 'hide';
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageNum = f_page;
            obj.pageSize = f_pageSize;
            //obj.userId=2;
            if(com_obj.userId && com_obj.userId>0){
                obj.userId = com_obj.userId;
            }else{
                obj.userId = 0;
            }
            obj.storeId= $.getStorage('mallStoreId');
            return obj;
        },
        visibleTime: 0,
        visible: ''+_visible+'',
        approve: true,
        api:"yg_baijiaplatform/orderObject/listOrderObjectDaiZhiFuByUserId",
        success: function (result){
            if (result.code == 10000) {
                var data = result.data;
                if (data.length > 0) {
                    $.each(data,function (index,item) {
                        var obj={};
                        obj.orderSn=item.orderSn;
                        obj.status=item.status;
                        obj.lian='./orderDetail.html?orderSn='+item.orderSn+'';
                        if(item.status==1){//待支付
                            obj.statusTex="待支付";
                            obj.lian='../pay/order_pay.html?orderSn='+item.orderSn+'';
                        }
                        var orders=[];
                        if(item.orderInfoVoList.length>0){
                            $.each(item.orderInfoVoList,function (j,rows) {
                                var gg = rows.listOrderDetail;
                                if(gg.length>0){
                                    $.each(gg,function (k,row) {
                                        var cobj={};
                                        if(row.navigateImg1){
                                            cobj.navigateImg1=row.navigateImg1;
                                        }
                                        cobj.goodsName=row.goodsName;
                                        cobj.specName=row.specName;
                                        cobj.quantity=row.quantity;
                                        cobj.salePrice=divideNumber(row.salePrice,100);//商品总价
                                        orders.push(cobj);
                                    })
                                }
                            });
                        }
                        obj.listOrderDetail=orders;
                        if(item.totalFee==0){
                            obj.totalFee = 0.01;
                        }else {
                            obj.totalFee=divideNumber(item.totalFee,100);//实付总计
                        }
                        app.payData.push(obj);
                    });
                    if(loading2){
                        loading2=false;
                    }
                }else{
                    loading2=true;
                    $(".tabs-2").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                        'style="display: inline-block;width: 113px;height: 100px;" ' +
                        'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
                    if(f_page>1){
                        YDUI.dialog.toast('没有数据了','none',function(){});
                        YDUI.dialog.loading.close();
                    }
                }
            } else if (result.code == 20100) {
                YDUI.dialog.toast('用户认证失败，请重新登录','error',function(){});
            } else if (result.code == 20141) {
                $(".tabs-2").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                    'style="display: inline-block;width: 113px;height: 100px;" ' +
                    'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
            } else {
                YDUI.dialog.toast(result.message,'error',function(){});
            }
        }
    });
}
//待发货
var d_page=1;
var d_pageSize=15;
var loading3=false;
function deliverGoods(){
    var _visible = 'show';
    if (loading3) {
        _visible = 'hide';
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageNum = d_page;
            obj.pageSize = d_pageSize;
            obj.status = 2;
            //obj.userId=2;
            if(com_obj.userId && com_obj.userId>0){
                obj.userId = com_obj.userId;
            }else{
                obj.userId = 0;
            }
            obj.storeId= $.getStorage('mallStoreId');
            return obj;
        },
        visibleTime: 0,
        visible: ''+_visible+'',
        approve: true,
        api:"yg_baijiaplatform/order/selectOrderInfoByStatusAndUserId",
        success: function (result) {
            if (result.code == 10000) {
                var data = result.data;
                if (data.length > 0) {
                    $.each(data,function (index,item) {
                        var obj={};
                        obj.orderSn=item.orderSn;
                        obj.status=item.status;
                        obj.butShow=false;
                        obj.lian='./orderDetail.html?orderSn='+item.orderSn+'';
                        if(item.status==2){//待发货
                            obj.statusTex="待发货";
                        }
                        if(item.totalFee == 0){
                            obj.totalFee= 0.01;
                        }else {
                            obj.totalFee=divideNumber(item.totalFee,100);//实付总计
                        }
                        var orders=[];
                        if(item.listOrderDetail.length>0){
                            $.each(item.listOrderDetail,function (j,rows) {
                                var cobj={};
                                if(rows.navigateImg1){
                                    cobj.navigateImg1=rows.navigateImg1;
                                }
                                cobj.goodsName=rows.goodsName;
                                cobj.specName=rows.specName;
                                cobj.quantity=rows.quantity;
                                cobj.salePrice=divideNumber(rows.salePrice,100);//商品总价
                                orders.push(cobj);
                            });
                        }
                        obj.deliveryWay=item.deliveryWay;
                        obj.listOrderDetail=orders;
                        app.sendData.push(obj);
                    });
                    if(loading3){
                        loading3=false;
                    }
                }else{
                    loading3=true;
                    $(".tabs-3").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                        'style="display: inline-block;width: 113px;height: 100px;" ' +
                        'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
                    if(d_page>1){
                        YDUI.dialog.toast('没有数据了~','none',function(){});
                        YDUI.dialog.loading.close();
                    }
                }
            } else if (result.code == 20100) {
                YDUI.dialog.toast('用户认证失败，请重新登录','error',function(){});
            } else if(result.code == 20141){
                $(".tabs-3").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                    'style="display: inline-block;width: 113px;height: 100px;" ' +
                    'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
            }else {
                YDUI.dialog.toast(result.message,'error',function(){});
            }
        }
    });
}
//待收货
var s_page=1;
var s_pageSize=15;
var loading4=false;
function receiveGoods(){
    var _visible = 'show';
    if (loading4) {
        _visible = 'hide';
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageNum = s_page;
            obj.pageSize = s_pageSize;
            obj.status = 3;
            //obj.userId=2;
            if(com_obj.userId && com_obj.userId>0){
                obj.userId = com_obj.userId;
            }else{
                obj.userId = 0;
            }
            obj.storeId= $.getStorage('mallStoreId');
            return obj;
        },
        visibleTime: 0,
        visible: ''+_visible+'',
        approve: true,
        api:"yg_baijiaplatform/order/selectOrderInfoByStatusAndUserId",
        success: function (result) {
            if (result.code == 10000) {
                var data = result.data;
                if (data.length > 0) {
                    $.each(data,function (index,item) {
                        var obj={};
                        obj.orderSn=item.orderSn;
                        obj.lian='./orderDetail.html?orderSn='+item.orderSn+'';
                        if(item.status==3){//待发货
                            obj.status=item.status;
                            obj.statusTex="待收货";
                        }
                        obj.deliveryWay=item.deliveryWay;
                        var orders=[];
                        if(item.listOrderDetail.length>0){
                            $.each(item.listOrderDetail,function (j,rows) {
                                var cobj={};
                                if(rows.navigateImg1){
                                    cobj.navigateImg1=rows.navigateImg1;
                                }
                                cobj.goodsName=rows.goodsName;
                                cobj.specName=rows.specName;
                                cobj.quantity=rows.quantity;
                                cobj.salePrice=divideNumber(rows.salePrice,100);//商品单价
                                orders.push(cobj);
                            });
                        }
                        obj.listOrderDetail=orders;
                        if(item.totalFee==0){
                            obj.totalFee=0.01;
                        }else {
                            obj.totalFee=divideNumber(item.totalFee,100);//商品总价
                        }
                        app.receiveData.push(obj);
                    });
                    if(loading4){
                        loading4=false;
                    }
                }else{
                    $(".tabs-4").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                        'style="display: inline-block;width: 113px;height: 100px;" ' +
                        'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
                    loading4=true;
                    if(s_page>1){
                        if(loading4){
                            YDUI.dialog.toast('没有数据了~','none',function(){});
                            YDUI.dialog.loading.close();
                        }

                    }
                }
            } else if (result.code == 20100) {
                YDUI.dialog.toast('用户认证失败，请重新登录','error',function(){});
            } else if(result.code == 20141){
                $(".tabs-4").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                    'style="display: inline-block;width: 113px;height: 100px;" ' +
                    'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
            }else {
                YDUI.dialog.toast(result.message,'error',function(){});
            }
        }
    });
}
//待评价
var fn_page=1;
var fn_pageSize=15;
var loading5=false;
function finishGoods(){
    var _visible = 'show';
    if (loading5) {
        _visible = 'hide';
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.pageNum = fn_page;
            obj.pageSize = fn_pageSize;
            obj.status = 4;
            //obj.userId=2;
            if(com_obj.userId && com_obj.userId>0){
                obj.userId = com_obj.userId;
            }else{
                obj.userId = 0;
            }
            obj.storeId= $.getStorage('mallStoreId');
            return obj;
        },
        visibleTime: 0,
        visible: ''+_visible+'',
        approve: true,
        api:"yg_baijiaplatform/order/selectOrderInfoByStatusAndUserId",
        success: function (result) {
            if (result.code == 10000) {
                var data = result.data;
                if (data.length > 0) {
                    $.each(data,function (index,item) {
                        var obj={};
                        obj.orderSn=item.orderSn;
                        obj.status=item.status;
                        obj.lian='./orderDetail.html?orderSn='+item.orderSn+'';
                        obj.statusTex="待评价";
                        obj.deliveryWay=item.deliveryWay;
                        var orders=[];
                        if(item.listOrderDetail.length>0){
                            $.each(item.listOrderDetail,function (j,rows) {
                                var cobj={};
                                if(rows.navigateImg1){
                                    cobj.navigateImg1=rows.navigateImg1;
                                }
                                cobj.goodsName=rows.goodsName;
                                cobj.specName=rows.specName;
                                cobj.quantity=rows.quantity;
                                cobj.salePrice=divideNumber(rows.salePrice,100);//商品单价
                                orders.push(cobj);
                            });
                        }
                        obj.listOrderDetail=orders;
                        if(item.totalFee==0){
                            obj.totalFee=0.01;
                        }else {
                            obj.totalFee=divideNumber(item.totalFee,100);//实付总计
                        }
                        app.evaluateData.push(obj);
                    });
                }else{
                    $(".tabs-5").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                        'style="display: inline-block;width: 113px;height: 100px;" ' +
                        'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
                    if(fn_page>1){
                        $("body").destroyInfinite();
                        YDUI.dialog.toast('没有数据了~','none',function(){});
                    }
                }
            } else if (result.code == 20100) {
                YDUI.dialog.toast('用户认证失败，请重新登录','error',function(){});
            } else if(result.code == 20141){
                $(".tabs-5").html('<div style="text-align: center;padding-top: 30px;"><img ' +
                    'style="display: inline-block;width: 113px;height: 100px;" ' +
                    'src="../images/shop/erro_dingdan@2x.png"><p style="font-size: 14px;color: #bbbbbb;">暂无待收货订单信息~</p></div>');
            }else {
                YDUI.dialog.toast(result.message,'error',function(){});
            }
        }
    });
}

//查看商品物流方式
function logisticsWays(type,item){
    if(type==3){
        //window.location.href='check_logistics.html?orderSn='+orderSn;
    }
}
//滚动加载
function infiniteSroll() {
    $("body").infinite().on("infinite", function () {
        var cindex = $(".order-all a[class='textshow']").index() + 1;
        if (cindex == 1) {//全部订单列表
            if (loading1) return;
            loading1 = true;
            YDUI.dialog.loading.open('正在加载...');
            setTimeout(function () {
                page1++;
                loading1 = false;
                orderAll();
            }, 1500);   //模拟延迟
        }else if(cindex == 2){//待付款订单列表
            if (loading2) return;
            loading2 = true;
            YDUI.dialog.loading.open('正在加载...');
            setTimeout(function () {
                f_page++;
                loading2 = false;
                payGoods();
            }, 1500);   //模拟延迟
        }else if(cindex == 3){//待发货订单列表
            if (loading3) return;
            loading3 = true;
            YDUI.dialog.loading.open('正在加载...');
            setTimeout(function () {
                d_page++;
                loading3 = false;
                deliverGoods();
            }, 1500);   //模拟延迟
        }else if(cindex == 4){//待收货订单列表
            if (loading4) return;
            loading4 = true;
            YDUI.dialog.loading.open('正在加载...');
            setTimeout(function () {
                s_page++;
                loading4 = false;
                receiveGoods();
            }, 1500);   //模拟延迟
        }else if(cindex == 5){//已完成订单列表
            if (loading5) return;
            loading5 = true;
            YDUI.dialog.loading.open('正在加载...');
            setTimeout(function () {
                fn_page++;
                loading5 = false;
                finishGoods();
            }, 1500);   //模拟延迟
        }
    })
}


//返回上一页
function returnBack() {
    window.history.back();  //返回上一页
}


//登录过期
function againAccessToken() {
    $.delStorage("userData");
    $.delStorage('userOpenid');
    $.delStorage("yd_wx");
    $.delStorage("yd_AccessTokenIndex");
    $.delStorage("userData");
    $.delStorage("uData");
    $.delStorage("AccessTokenIndex");

    //wxAccredit();
}